local items = {
  "转换技，",
  "①当你成为牌的目标后，你可以令一名本回合失去过牌的角色摸一张牌。",
  "②当你受到伤害后，伤害来源可以将手牌数摸至体力上限，然后你回复1点体力。",
  "<font color='#E0DB2F'>①当你成为牌的目标后，你可以令一名本回合失去过牌的角色摸一张牌。</font>",
  "<font color='#E0DB2F'>②当你受到伤害后，伤害来源可以将手牌数摸至体力上限，然后你回复1点体力。</font>",
}

Fk:loadTranslationTable{
  ["plum__zaichen"] = "再臣",
  [":plum__zaichen"] = "转换技，①当你成为牌的目标后，你可以令一名本回合失去过牌的角色摸一张牌。②当你受到伤害后，伤害来源可以将手牌数摸至体力上限，然后你回复1点体力。",

  [":plum__zaichen_yang"] = items[1] .. items[4] .. items[3],
  [":plum__zaichen_yin"] = items[1] .. items[5] .. items[2],

  ["#plum__zaichen-choose"] = "再臣：可以令一名本回合失去过牌的角色摸一张牌",
  ["#plum__zaichen-invoke"] = "再臣：可以将手牌摸至体力上限并令 %dest 回复1点体力",
}

local T = require "packages.tuntian_studio.utility"

---@param room Room
---@return ServerPlayer[]
local function findLosers(room)
  local players = {}
  room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function (e)
    for _, move in ipairs(e.data) do
      if players == room.alive_players then break end
      local to_same = move.from and move.to and move.from == move.to
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea <= Card.PlayerEquip then
          if not to_same or (to_same and move.toArea >= Card.PlayerJudge) then
            table.insertIfNeed(players, move.from)
          end
        end
      end
    end
  end, Player.HistoryTurn)
  return table.filter(players, function(p) return not p.dead end)
end

local zaichen = fk.CreateSkill {
  name = "plum__zaichen",
  tags = {Skill.Switch},
}

--- Yang
zaichen:addEffect(fk.TargetConfirmed, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(zaichen.name) and player:getSwitchSkillState(zaichen.name) == fk.SwitchYang then
      return player == target and #findLosers(player.room) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = findLosers(room),
      skill_name = zaichen.name,
      prompt = "#plum__zaichen-choose",
      cancelable = true,
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local tos = event:getCostData(self).tos ---@type ServerPlayer[]
    tos[1]:drawCards(1, zaichen.name)
  end,
})

--- Yin
zaichen:addEffect(fk.Damaged, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(zaichen.name) and player:getSwitchSkillState(zaichen.name) == fk.SwitchYin then
      return player == target and data.from and not data.from.dead
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(data.from, {
      skill_name = zaichen.name,
      prompt = "#plum__zaichen-invoke::" .. player.id,
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    T.adjustHandcardNumTo(data.from, math.max(data.from.maxHp, data.from:getHandcardNum()), zaichen.name)
    if player.dead then return end
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = zaichen.name
    })
  end,
})

return zaichen